package cn.gudian.cms.System

class PageController {

  def index = { redirect(action: "list", params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    [pageInstanceList: Page.list(params), pageInstanceTotal: Page.count()]
  }

  def create = {
    def path = params.path
    def pageInstance = new Page()
    pageInstance.properties = params
    return [pageInstance: pageInstance,path:path]
  }

  def save = {
    def pageInstance = new Page(params)
    pageInstance.uptime = new Date();
    pageInstance.senddate = new Date();
    if (!pageInstance.hasErrors() && pageInstance.save()) {
      flash.message = "page.created"
      flash.args = [pageInstance.id]
      flash.defaultMessage = "Page ${pageInstance.id} created"
      redirect(action: "edit", id: pageInstance.id)
    }
    else {
      render(view: "create", model: [pageInstance: pageInstance])
    }
  }

  def show = {
    def pageInstance = Page.get(params.id)
    if (!pageInstance) {
      flash.message = "page.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "Page not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [pageInstance: pageInstance]
    }
  }

  def edit = {
    def pageInstance
    if(params.id){
        pageInstance = Page.get(params.id)
    }else{
        pageInstance = Page.findByPath(params.path)
    }

    if (!pageInstance && !params.path) {
      flash.message = "page.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "Page not found with id ${params.id}"
      redirect(action: "list")
    }else if(!pageInstance && params.path){
       redirect(action: "create", params: [path: params.path])
    }
    else {
      return [pageInstance: pageInstance]
    }
  }

  def update = {
    def pageInstance = Page.get(params.id)
    if (pageInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (pageInstance.version > version) {

          pageInstance.errors.rejectValue("version", "page.optimistic.locking.failure", "Another user has updated this Page while you were editing")
          render(view: "edit", model: [pageInstance: pageInstance])
          return
        }
      }
      pageInstance.properties = params
      pageInstance.uptime = new Date();
      if (!pageInstance.hasErrors() && pageInstance.save()) {
        flash.message = "page.updated"
        flash.args = [params.id]
        flash.defaultMessage = "Page ${params.id} updated"
        redirect(action: "edit", id: pageInstance.id)
      }
      else {
        render(view: "edit", model: [pageInstance: pageInstance])
      }
    }
    else {
      flash.message = "page.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "Page not found with id ${params.id}"
      redirect(action: "edit", id: params.id)
    }
  }

  def delete = {
    def pageInstance = Page.get(params.id)
    if (pageInstance) {
      try {
        pageInstance.delete()
        flash.message = "page.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "Page ${params.id} deleted"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "page.not.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "Page ${params.id} could not be deleted"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "page.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "Page not found with id ${params.id}"
      redirect(action: "list")
    }
  }

  def deleteAll = {
    def qstr = params.qstr
    if (qstr) {
      try {
        Page.executeUpdate("delete Page where id in(" + qstr + ")")
        flash.message = "delete successfully:" + qstr
        redirect(action: "list")
      } catch (Exception e) {
        flash.message = "delete failed:" + qstr
        redirect(action: "list")
      }
    }
  }
}
